package com.shanhesoft.java.modules.exam.handler;

import com.shanhesoft.java.common.core.util.SpringContextHolder;
import com.shanhesoft.java.modules.exam.domain.Answer;
import com.shanhesoft.java.modules.exam.domain.dto.*;
import com.shanhesoft.java.modules.exam.enums.SubjectTypeEnum;
import com.shanhesoft.java.modules.exam.service.*;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 统计成绩
 * @author tangyi
 * @date 2020/1/19 10:07 上午
 */
public abstract class AbstractAnswerHandler implements IAnswerHandler {


	@Override
	public AnswerHandleResult handle(List<Answer> answers) {
		if (CollectionUtils.isNotEmpty(answers)) {
			// 保存答题正确的题目分数
			List<Integer> rightScore = new ArrayList<>();
			// 获取题目信息
			List<SubjectDto> subjects = getSubjects(answers);
			answers.forEach(tempAnswer -> {
				subjects.stream()
						// 题目ID匹配
						.filter(tempSubject -> tempSubject.getId().equals(tempAnswer.getQueId())).findFirst()
						.ifPresent(subject -> judge(tempAnswer, subject, rightScore));
			});
			AnswerHandleResult result = new AnswerHandleResult();
			// 记录总分、正确题目数、错误题目数
			result.setScore(rightScore.stream().mapToDouble(Double::valueOf).sum());
			result.setCorrectNum(rightScore.size());
			result.setInCorrectNum(answers.size() - rightScore.size());
			return result;
		}
		return null;
	}

	@Override
	public List<SubjectDto> getSubjects(List<Answer> answers) {
		List<SubjectDto> subjectDtoList = new ArrayList<>();
		// 构造dto
		for(Answer answer : answers) {
			if(null == answer.getAnswer()) {
				answer.setAnswer("");
			}

			SubjectDto subjectDto = new SubjectDto();

			SubjectTypeEnum subjectType = SubjectTypeEnum.matchByValue(Integer.parseInt(""+answer.getQueType()));
			if (subjectType != null) {
				switch (subjectType) {
					case CHOICES:
						TBankSingleChoiceQueDto singleChoiceQueDto = SpringContextHolder.getApplicationContext().getBean(TBankSingleChoiceQueService.class).getOneDto(answer.getQueId());
						subjectDto.setAnalysis(singleChoiceQueDto.getAnswerExplain());
						subjectDto.setAnswer(singleChoiceQueDto.getAnswer());
						//subjectDto.setChoicesType(0); // 单选
						subjectDto.setQueId(singleChoiceQueDto.getId());
						subjectDto.setQueType(subjectType.getValue());
						subjectDto.setExamRecordId(answer.getExamRecordId());
						subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
						subjectDto.setPaperId(""); // 似乎可以不用设置
						subjectDto.setQueName(singleChoiceQueDto.getContent());
						subjectDto.setScore(answer.getScore());
						// 构造option
						List<SubjectOption> singleOptionList = new ArrayList<>();
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceA())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceA());
							option.setOptionName("A");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceB())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceB());
							option.setOptionName("B");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceC())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceC());
							option.setOptionName("C");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceD())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceD());
							option.setOptionName("D");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceE())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceE());
							option.setOptionName("E");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceF())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceF());
							option.setOptionName("F");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceG())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(singleChoiceQueDto.getChoiceG());
							option.setOptionName("G");
							// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(singleChoiceQueDto.getId());
							singleOptionList.add(option);
						}

						subjectDto.setOptions(singleOptionList);
						break;
					case MULTIPLE_CHOICES:
						TBankMultipleChoiceQueDto multipleChoiceQueDto = SpringContextHolder.getApplicationContext().getBean(TBankMultipleChoiceQueService.class).getOneDto(answer.getQueId());
						subjectDto.setAnalysis(multipleChoiceQueDto.getAnswerExplain());
						subjectDto.setAnswer(multipleChoiceQueDto.getAnswer());
						//subjectDto.setChoicesType(0); // 单选
						subjectDto.setQueId(multipleChoiceQueDto.getId());
						subjectDto.setQueType(subjectType.getValue());
						subjectDto.setExamRecordId(answer.getExamRecordId());
						subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
						subjectDto.setPaperId(""); // 似乎可以不用设置
						subjectDto.setQueName(multipleChoiceQueDto.getContent());
						subjectDto.setScore(answer.getScore());
						// 构造option
						List<SubjectOption> multipleOptionList = new ArrayList<>();
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceA())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceA());
							option.setOptionName("A");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceB())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceB());
							option.setOptionName("B");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceC())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceC());
							option.setOptionName("C");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceD())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceD());
							option.setOptionName("D");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceE())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceE());
							option.setOptionName("E");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceF())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceF());
							option.setOptionName("F");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}
						if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceG())) {
							SubjectOption option = new SubjectOption();
							option.setOptionContent(multipleChoiceQueDto.getChoiceG());
							option.setOptionName("G");
//							if(answer.getAnswer().equals(option.getOptionName())) {
//								option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//							}
							option.setSubjectChoicesId(multipleChoiceQueDto.getId());
							multipleOptionList.add(option);
						}

						subjectDto.setOptions(multipleOptionList);
						break;
					case JUDGEMENT:
						TBankJudgeQueDto judgeQueDto = SpringContextHolder.getApplicationContext().getBean(TBankJudgeQueService.class).getOneDto(answer.getQueId());
						subjectDto.setAnalysis(judgeQueDto.getAnswerExplain());
						subjectDto.setAnswer(judgeQueDto.getAnswer());
						//subjectDto.setChoicesType(0); // 单选
						subjectDto.setQueId(judgeQueDto.getId());
						subjectDto.setQueType(subjectType.getValue());
						subjectDto.setExamRecordId(answer.getExamRecordId());
						subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
						subjectDto.setPaperId(""); // 似乎可以不用设置
						subjectDto.setQueName(judgeQueDto.getContent());
						subjectDto.setScore(answer.getScore());
						// 构造option
						List<SubjectOption> fillOptionList = new ArrayList<>();
						SubjectOption optionRight = new SubjectOption();
						optionRight.setOptionContent("T");
						optionRight.setOptionName("");
//						if(answer.getAnswer().equals(optionRight.getOptionContent())) {
//							optionRight.setRight(judgeQueDto.getAnswer().equals(optionRight.getOptionContent())? TRUE : FALSE);
//						}
						optionRight.setSubjectChoicesId(judgeQueDto.getId());
						fillOptionList.add(optionRight);

						SubjectOption optionError = new SubjectOption();
						optionError.setOptionContent("F");
						optionError.setOptionName("");
//						if(answer.getAnswer().equals(optionError.getOptionContent())) {
//							optionError.setRight(judgeQueDto.getAnswer().equals(optionError.getOptionContent())? TRUE : FALSE);
//						}
						optionError.setSubjectChoicesId(judgeQueDto.getId());
						fillOptionList.add(optionError);

						subjectDto.setOptions(fillOptionList);
						break;
					case SHORT_ANSWER:
						TBankFillQueDto fillQueDto = SpringContextHolder.getApplicationContext().getBean(TBankFillQueService.class).getOneDto(answer.getQueId());
						subjectDto.setAnalysis(fillQueDto.getAnswerExplain());
						subjectDto.setAnswer(fillQueDto.getAnswer());
						//subjectDto.setChoicesType(0); // 单选
						subjectDto.setQueId(fillQueDto.getId());
						subjectDto.setQueType(subjectType.getValue());
						subjectDto.setExamRecordId(answer.getExamRecordId());
						subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
						subjectDto.setPaperId(""); // 似乎可以不用设置
						subjectDto.setQueName(fillQueDto.getContent());
						subjectDto.setScore(answer.getScore());
						break;
					case PROBLEMS_ANSWER:
						OjProblemsDto problemsDto = SpringContextHolder.getApplicationContext().getBean(OjProblemsService.class).getOneDto(answer.getQueId());
						subjectDto.setAnalysis(problemsDto.getProblemHint());
						subjectDto.setAnswer(problemsDto.getProblemAnswer());
						//subjectDto.setChoicesType(0); // 单选
						subjectDto.setQueId(problemsDto.getId());
						subjectDto.setQueType(subjectType.getValue());
						subjectDto.setExamRecordId(answer.getExamRecordId());
						subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
						subjectDto.setPaperId(""); // 似乎可以不用设置
						subjectDto.setQueName(problemsDto.getProblemName());
						subjectDto.setScore(answer.getScore());

						break;
					default:
						break;
				}
			}

			subjectDtoList.add(subjectDto);
		}

		return subjectDtoList;
	}

}
